includes KrzytomMsg;

module KrzytomSerialM
{
  provides interface StdControl;
  uses {
    interface Leds;
    interface StdControl as RadioCommControl;
    interface StdControl as UARTCommControl;
    interface SendMsg as UARTSend;
    interface ReceiveMsg as RadioReceive;
  }
}
implementation
{
  //TOS_Msg msg[2];
  //uint8_t currentMsg;
  TOS_MsgPtr msg;

  command result_t StdControl.init() {
    call Leds.init();
    call Leds.yellowOff(); call Leds.redOff(); call Leds.greenOff();

    call RadioCommControl.init();
    call UARTCommControl.init();
    
    /*atomic {
      currentMsg = 0;
    }*/
    
    return SUCCESS;
  }

  command result_t StdControl.start() {
    call UARTCommControl.start();
    call RadioCommControl.start();
    return SUCCESS;
  }

  command result_t StdControl.stop() {
    call UARTCommControl.stop();
    call RadioCommControl.stop();
    return SUCCESS;
  }

  task void dataTask() {

    //if (call DataMsg.send(TOS_UART_ADDR, sizeof(struct OscopeMsg), &msg[currentMsg])) {
    if (call UARTSend.send(TOS_UART_ADDR, sizeof(struct KrzytomMsg), &msg)) {
		/*atomic {
		  currentMsg ^= 0x1;
		}*/
		call Leds.yellowToggle();
    }
  }

  event result_t UARTSend.sendDone(TOS_MsgPtr sent, result_t success) {
    return SUCCESS;
  }

  event TOS_MsgPtr RadioReceive.receive(TOS_MsgPtr m) {
    TOS_MsgPtr tmp;
    tmp = msg;
    msg = m;

	post dataTask();
	
    return m;
  }
}
